package com.hmall.order.config;

import com.hmall.order.constant.MqConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Ronin
 * @date 2023/4/10 17:21
 * @Version 1.0
 * @Description:Rabbit配置类
 */
@Configuration
public class RabbitConfig {

    /**
     * 声明交换机
     * @return
     */
    @Bean
    public TopicExchange createExchange() {
        return new TopicExchange(MqConstant.ORDER_EXCHANGE, true, false);
    }

    /**
     * 声明延迟队列
     * @return
     */
    @Bean
    public Queue createDelayQueue() {
        return QueueBuilder.durable(MqConstant.ORDER_QUEUE)
                // .ttl(1800000)//指定延迟时间，单位毫秒
                .ttl(200000)//测试
                .deadLetterExchange(MqConstant.ORDER_DL_EXCHANGE)
                .deadLetterRoutingKey(MqConstant.ORDER_DL_KEY)
                .build();
    }

    /**
     * 声明死信交换机
     * @return
     */
    @Bean
    public TopicExchange createDeadLetterExchange() {
        return new TopicExchange(MqConstant.ORDER_DL_EXCHANGE, true, false);
    }

    /**
     * 声明死信队列
     * @return
     */
    @Bean
    public Queue createDeadLetterQueue() {
        return QueueBuilder.durable(MqConstant.ORDER_DL_QUEUE).build();
    }

    /**
     * 绑定交换机与延迟队列
     * @return
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(createDelayQueue()).to(createExchange()).with(MqConstant.ORDER_KEY);
    }

    /**
     * 绑定死信交换机与死信队列
     * @return
     */
    @Bean
    public Binding bindingDeadLetter() {
        return BindingBuilder.bind(createDeadLetterQueue()).to(createDeadLetterExchange()).with(MqConstant.ORDER_DL_KEY);
    }
}
